#ifndef __H_ONLINE__
#define __H_ONLINE__
#include <mutex>
#include <unordered_map>
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>
typedef websocketpp::server<websocketpp::config::asio> wsserver_t;

class online_user_manage
{
    public:
        bool enter_game_hall(uint64_t uid,wsserver_t::connection_ptr& conn)
        {
            std::unique_lock<std::mutex> lock(_mutex);
            _hall_user[uid] = conn;
            return true;
        }
        bool enter_game_room(uint64_t uid,wsserver_t::connection_ptr& conn)
        {
            std::unique_lock<std::mutex> lock(_mutex);
            _room_user[uid] = conn;
        }
        bool exit_game_hall(uint64_t uid)
        {
            std::unique_lock<std::mutex> lock(_mutex);
            _hall_user.erase(uid);
        }
        bool exit_game_room(uint64_t uid)
        {
            std::unique_lock<std::mutex> lock(_mutex);
            _room_user.erase(uid);
        }

        //判断当前指定用户是否在游戏大厅/游戏房间
        bool is_in_game_hall(uint64_t uid)
        {
            std::unique_lock<std::mutex> lock(_mutex);
            return _hall_user.count(uid);
        }
        bool is_in_game_room(uint64_t uid)
        {
            std::unique_lock<std::mutex> lock(_mutex);
            return _room_user.count(uid);
        }
        //通过用户ID在游戏大厅/游戏房间用户管理中获取对应的通信连接
        wsserver_t::connection_ptr get_conn_from_hall(uint64_t uid)
        {
            std::unique_lock<std::mutex> lock(_mutex);
            if(!_hall_user.count(uid))
            {
                return wsserver_t::connection_ptr();
            }
            return _hall_user[uid];
        }
        wsserver_t::connection_ptr get_conn_from_room(uint64_t uid)
        {
            std::unique_lock<std::mutex> lock(_mutex);
            if(!_room_user.count(uid))
            {
                return wsserver_t::connection_ptr();
            }
            return _room_user[uid];
        }
    private:
        std::mutex _mutex;
        std::unordered_map<uint64_t,wsserver_t::connection_ptr> _hall_user;
        std::unordered_map<uint64_t,wsserver_t::connection_ptr> _room_user;
};

#endif